package com.activiti.dome.controller;

import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.bpmn.data.Data;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author Zemin.Yang
 */
@RestController
public class ProcessEngineController {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private ProcessRuntime processRuntime;

    @GetMapping("management/properties")
    public Object findAllService() {
        return processEngine.getManagementService().getProperties();
    }

    @GetMapping("process/runtime/processDefinitions")
    public void processDefinitions() {
        List<ProcessDefinition> list = processEngine.getRepositoryService().createProcessDefinitionQuery().list();
        if (list != null && list.size() > 0) {
            for (ProcessDefinition processDefinition : list) {
                System.out.println("流程定义ID:" + processDefinition.getId());//流程定义的key+版本+随机生成数
                System.out.println("流程定义名称:" + processDefinition.getName());//对应HelloWorld.bpmn文件中的name属性值
                System.out.println("流程定义的key:" + processDefinition.getKey());//对应HelloWorld.bpmn文件中的id属性值
                System.out.println("流程定义的版本:" + processDefinition.getVersion());//当流程定义的key值相同的情况下，版本升级，默认从1开始
                System.out.println("资源名称bpmn文件:" + processDefinition.getResourceName());
                System.out.println("资源名称png文件:" + processDefinition.getDiagramResourceName());
                System.out.println("部署对象ID:" + processDefinition.getDeploymentId());
                System.out.println("################################");
            }
        }
    }

    /**
     * 查询用户待办任务列表。
     */
    @GetMapping("process/runtime/tasks")
    public void tasks() {
        List<Task> list = processEngine.getTaskService().createTaskQuery().list();
        for (Task task : list) {
            System.out.println("任务ID：" + task.getId());

            System.out.println("任务名称:" + task.getName());

            System.out.println("任务的创建时间:" + task.getCreateTime());

            System.out.println("任务的办理人:" + task.getAssignee());

            System.out.println("流程实例ID:" + task.getProcessInstanceId());

            System.out.println("执行对象ID:" + task.getExecutionId());

            System.out.println("流程定义ID:" + task.getProcessDefinitionId());

            System.out.println("****************************");
        }
    }

    /**
     * 查询已处理任务列表。
     */
    @GetMapping("process/runtime/historyServiceTaskInstance")
    public void historyServiceTaskInstance(String user) {
        List<HistoricTaskInstance> list = processEngine.getHistoryService().createHistoricTaskInstanceQuery()
            .taskAssignee(user)
            .finished()
            .list();
        for (HistoricTaskInstance historicTaskInstance : list) {
            System.out.println("taskId: " + historicTaskInstance.getId());
            System.out.println("taskName: " + historicTaskInstance.getName());
            System.out.println("processDefinitionId: " + historicTaskInstance.getProcessInstanceId());
        }
    }


    @GetMapping("process/runtime/processInstance")
    public void processInstances() {
        List<ProcessInstance> list = processEngine.getRuntimeService().createProcessInstanceQuery().list();
    }

    @GetMapping("process/runtime/startProcessInstanceByKey")
    public Object startProcessInstanceByKey(String key, String applyname) {
        Map<String, Object> variables = new HashMap<>(1);
        variables.put("applyname", applyname);
        variables.put("SB", Arrays.asList("1","2",new Date()));
        return processEngine.getRuntimeService().startProcessInstanceByKey(key, variables).toString();
    }

    @GetMapping("process/repository/deploy")
    public Object deploy(String path, String name) {
        return processEngine.getRepositoryService().createDeployment()
            .addClasspathResource(path).name(name).deploy();
    }

    /**
     * 办理任务
     */
    @GetMapping("process/task/complete")
    public void processTaskComplete(String taskId) {
        processEngine.getTaskService().complete(taskId);
    }
}
